/********************************************
*  test_FreeQueue.c
********************************************/


#ifdef __cplusplus
 #if __cplusplus
  extern "C"{
 #endif
#endif /* __cplusplus */


#include "CUnit/CUnit.h"
#include "CUnit/Basic.h"

#include "d_freeque.h"
#include "d_task.h"

#if 0

UINT32 TEST_DMS_FreQueCreate_00()
{
    UINT32 uil;
    UINT32 uiRet;
    FQUE_HANDLE FQHandle;
    UINT32 uiTemp;
    UINT32 uij;

    uiRet = DMS_FreQueCreate( &FQHandle, 1000, sizeof(UINT32));
    CU_ASSERT_EQUAL(DMS_OK, uiRet);

    for(uij = 0 ; uij < 1000 ; uij++)
    {
        uiRet = DMS_FreQueRead(FQHandle, &uiTemp);
        CU_ASSERT_EQUAL(DMS_ERR_FREQUE_NO_MSG, uiRet);

        for( uil = 0 ; uil < 1000 ; uil++ )
        {
            uiRet = DMS_FreQueWrite(FQHandle, &uil);
            CU_ASSERT_EQUAL(DMS_OK, uiRet);
        }

        uiRet = DMS_FreQueWrite(FQHandle, &uiTemp);
        CU_ASSERT_EQUAL(DMS_ERR_FREQUE_FULL, uiRet);

        for( uil = 0 ; uil < 1000 ; uil++ )
        {
            uiRet = DMS_FreQueRead(FQHandle, &uiTemp);
            CU_ASSERT_EQUAL(DMS_OK, uiRet);

            if(uiTemp != uil)
            {
                printf("\r\n uiTemp = %u , uil = %u \r\n",uiTemp,uil);
            }

            CU_ASSERT_EQUAL(uiTemp, uil);
        }

        uiRet = DMS_FreQueRead(FQHandle, &uiTemp);
        CU_ASSERT_EQUAL(DMS_ERR_FREQUE_NO_MSG, uiRet);

    }


    uiRet = DMS_FreQueDelete(FQHandle);
    CU_ASSERT_EQUAL(DMS_OK, uiRet);

    return DMS_OK;
}


typedef struct tagtestMsg {
    UINT32 checksum;
    UINT32 auiData[10];
}TEST_MSG;

UINT32 uiTaskFlag = DMS_FALSE;

VOID TestMsgInit(TEST_MSG * pstMsg)
{
    UINT32 uil;
    UINT32 uiCount = 0;

    for( uil = 0 ; uil < 10; uil++ )
    {
        pstMsg->auiData[uil] = rand();
        uiCount += pstMsg->auiData[uil];
    }

    pstMsg->checksum = uiCount;
}

VOID TestMsgFini(TEST_MSG * pstMsg)
{
    UINT32 uil;
    UINT32 uiCount = 0;

    for( uil = 0 ; uil < 10; uil++ )
    {
        uiCount += pstMsg->auiData[uil];
    }

    CU_ASSERT_EQUAL(uiCount, pstMsg->checksum);
}


VOID FreQueTask(UINT32 ulParam,VOID * pParam)
{
    FQUE_HANDLE FQHandle = pParam;
    UINT32 uiRet;
    UINT32 uil;
    TEST_MSG stTemp;

    while(uiTaskFlag)
    {
        for( uil = 0 ; uil < ulParam ; uil++ )
        {
            TestMsgInit(&stTemp);
            uiRet = DMS_FreQueWrite(FQHandle, &stTemp);
            CU_ASSERT_EQUAL(DMS_OK, uiRet);
        }

        for( uil = 0 ; uil < ulParam ; uil++ )
        {
            uiRet = DMS_FreQueRead(FQHandle, &stTemp);
            CU_ASSERT_EQUAL(DMS_OK, uiRet);
            TestMsgFini(&stTemp);
        }
    }
}



UINT32 TEST_DMS_FreQueCreate_01()
{
    UINT32 uiRet;
    FQUE_HANDLE FQHandle;
    UINT32 uiTemp;
    UINT32 uij;
    TASK_HANDLE TaskHandle[10];
    CHAR   acTaskName[32];

    uiRet = DMS_FreQueCreate( &FQHandle, 1000, sizeof(TEST_MSG));
    CU_ASSERT_EQUAL(DMS_OK, uiRet);

    uiTaskFlag = DMS_TRUE;
    for(uij = 0 ; uij < 2 ; uij++)
    {
        sprintf(acTaskName,"task_%u",uij);
        uiRet = DMS_TaskCreate( &TaskHandle[uij],
                                acTaskName,
                                FreQueTask,
                                uij + 1,
                                FQHandle);

        CU_ASSERT_EQUAL(DMS_OK, uiRet);
    }

    DMS_TaskDelay(5000);
    uiTaskFlag = DMS_FALSE;

    for(uij = 0 ; uij < 2 ; uij++)
    {
        while(DMS_OK == DMS_TaskHandleValid(TaskHandle[uij]))
        {
            DMS_TaskDelay(10);
        }
    }

    uiRet = DMS_FreQueDelete(FQHandle);
    CU_ASSERT_EQUAL(DMS_OK, uiRet);

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueCreate_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueCreate_03()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueRead_00()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueRead_01()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueRead_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueRead_03()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueWrite_00()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueWrite_01()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueWrite_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueWrite_03()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueDelete_00()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueDelete_01()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueDelete_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FreQueDelete_03()
{

    return DMS_OK;
}

VOID TEST_DMS_FreQueCreate()
{
    //CU_ASSERT_EQUAL(TEST_DMS_FreQueCreate_00(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueCreate_01(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueCreate_02(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueCreate_03(),DMS_OK);
}


VOID TEST_DMS_FreQueRead()
{
    CU_ASSERT_EQUAL(TEST_DMS_FreQueRead_00(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueRead_01(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueRead_02(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueRead_03(),DMS_OK);
}


VOID TEST_DMS_FreQueWrite()
{
    CU_ASSERT_EQUAL(TEST_DMS_FreQueWrite_00(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueWrite_01(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueWrite_02(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueWrite_03(),DMS_OK);
}


VOID TEST_DMS_FreQueDelete()
{
    CU_ASSERT_EQUAL(TEST_DMS_FreQueDelete_00(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueDelete_01(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueDelete_02(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_FreQueDelete_03(),DMS_OK);
}

VOID TEST_suite_FreeQueue(VOID)
{
    CU_pSuite suite;
    suite = CU_add_suite(__FUNCTION__, NULL, NULL);
    CU_ADD_TEST(suite, TEST_DMS_FreQueCreate);
    //CU_ADD_TEST(suite, TEST_DMS_FreQueRead);
    //CU_ADD_TEST(suite, TEST_DMS_FreQueWrite);
    //CU_ADD_TEST(suite, TEST_DMS_FreQueDelete);
}

#endif

#ifdef __cplusplus
 #if __cplusplus
  }
 #endif
#endif /* __cplusplus */
